package com.cloudpivot.receivales.meta.service.impl;

import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson.JSONObject;
import com.cloudpivot.receivales.meta.service.FileHandleService;

import lombok.extern.slf4j.Slf4j;


import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;

import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.io.*;
import java.nio.charset.Charset;


/**
 *
 */
@Service
@Slf4j
public class FileHandleServiceImpl implements FileHandleService {

    @Value("${pdfcov.wordpath}")
    private String WORDPATH;

    @Value("${pdfcov.excelpath}")
    private String EXCELPATH;

    @Autowired
    private CloseableHttpClient httpClient;

    @Autowired
    private RequestConfig config;

    @Override
    public byte[] fileToPdf(File file, String fileType) {
        byte[] rsp = requestApiRest(file, fileType);
        return rsp;
    }

    /**
     * 请求服务
     * 增加 文件类型
     */
//    private byte[] requestApi(File file) {
//        try {
//            log.info("开始请求转换pdf");
//            // 声明 http get 请求
//            HttpPost httpPost = new HttpPost(PATH);
//            // 装载配置信息
//            httpPost.setConfig(config);
//            //设置头
//            //请求form-data
//            log.info("开始创建请求");
//            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
//            entityBuilder.setContentType(ContentType.MULTIPART_FORM_DATA);
//            entityBuilder.setCharset(Charset.forName("utf-8"));
//            entityBuilder.addBinaryBody("file", file);
//            HttpEntity httpEntity = entityBuilder.build();
//            httpPost.setEntity(httpEntity);
//            log.info("封装请求完成");
//            // 发起请求
//            CloseableHttpResponse response = httpClient.execute(httpPost);
//
//
//            // 判断状态码是否为200
//            if (response.getStatusLine().getStatusCode() == 200) {
//                log.info("请求转换pdf完成");
//                // 返回响应体的内容
//                return EntityUtils.toByteArray(response.getEntity());
//            }else{
//                log.info("请求异常-->{}", JSONObject.toJSONString(response.getStatusLine()));
//                return null;
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//            return null;
//        }
//
//    }
    private byte[] requestApiRest(File file, String fileType) {
        try {
            String realPath = "";
            if (fileType.equals("docx")) {
                realPath = WORDPATH;
            } else if (fileType.equals("xlsx")) {
                realPath = EXCELPATH;
            }else{
                return null;
            }
            HttpHeaders headers = new HttpHeaders();
            MediaType type = MediaType.parseMediaType("multipart/form-data");
            headers.setContentType(type);

            MultiValueMap<String, Object> multiValueMap = new LinkedMultiValueMap<>();
            multiValueMap.add("file", new FileSystemResource(file.getAbsolutePath()));
            HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(multiValueMap, headers);
            RestTemplate restTemplate = new RestTemplate();
            Resource result = restTemplate.postForObject(realPath, httpEntity, Resource.class);
            InputStream in = result.getInputStream();
            byte[] ret = IoUtil.readBytes(in);
            return ret;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
